from paho.mqtt import client as mqtt_client

broker = '127.0.0.1'
port = 1883
topic = "xiaomi/home1/control/led/off"
client_id = f'python-mqtt'

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)
    # Set Connecting Client ID
    client = mqtt_client.Client(client_id)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

client = connect_mqtt()
client.loop_start()
result = client.publish(topic)
print(result)
status = result[0]
if status == 0:
    print(f"Send to topic `{topic}`")
else:
    print(f"-----------Failed to send message to topic {topic}")